package com.lzb.video.utils;

import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.util.Log;
import android.view.SurfaceHolder;

import java.io.File;
import java.io.IOException;

/**
 * MediaRecorder 帮助类
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/10/14 17:05
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLKY  Co., Ltd. Inc. All rights reserved.
 */
public class MediaRecorderHelpUtil {

    private static final String TAG = MediaRecorderHelpUtil.class.getName();
    public static  boolean  DEBUG = true;
    /** 视频文件*/
    private static File file;

    /**
     * 初始化 MediaRecorder 对象实例
     * @param camera 摄像头
     * @param profile 摄像头配置文件
     * @param videoPath 视频保存的位置
     * @param holder SurfaceHolder 对象实例
     */
    public static MediaRecorder record( Camera camera , CamcorderProfile profile , String videoPath, SurfaceHolder holder){
        MediaRecorder  mediaRecorder = new MediaRecorder();
        //关闭锁，使其他线程可以访问摄像头 在打开摄像头时要打开锁
        camera.unlock();
        mediaRecorder.setCamera(camera);
        // 设置录制视频源为Camera(相机)
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        // 设置音频资源
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //Uses the settings from a CamcorderProfile object for recording  根据CamcorderProfile对象设置录制信息
        mediaRecorder.setProfile(profile);
        //设置视频大小（分辨率）
        mediaRecorder.setVideoSize(1280, 720);
        // 设置视频一次写多少字节(可调节视频空间大小)
        mediaRecorder.setVideoEncodingBitRate(1024 * 1024);
        //每秒4帧
        mediaRecorder.setVideoFrameRate(4);

        //设置视频文件的输出格式 必须在设置声音编码格式、图像编码格式之前设置
//        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

        // 最大期限
        mediaRecorder.setMaxDuration(35 * 1000);
        //视频文件名
        String fileName = "VID_" +  System.currentTimeMillis() + ".mp4";
        file = new File(videoPath, fileName);
        // 指定输出文件 ， 设置视频文件输出的路径
        mediaRecorder.setOutputFile(file.getAbsolutePath());
        //设置 surface 预览视频
        mediaRecorder.setPreviewDisplay(holder.getSurface());
        // // 设置保存录像方向
        mediaRecorder.setOrientationHint(90);

        mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
            @Override
            public void onError(MediaRecorder mr, int what, int extra) {
//                    Toast.makeText(RecorderVideoActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
            }
        });

        mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {

            }
        });

        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            Log.d(TAG,e.getMessage());
        }

        return mediaRecorder;
    }

    /**
     * 开始录制
     */
    public static void record(MediaRecorder mediaRecorder ){
        if (null == mediaRecorder){
            printMessage("mediaRecorder can not null !");
            return;
        }
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            Log.d(TAG,e.getMessage());
        }
    }

    public static void stop(MediaRecorder mediaRecorder){
        if (null == mediaRecorder){
            printMessage("mediaRecorder can not null !");
            return;
        }
        mediaRecorder.setOnInfoListener(null);
        mediaRecorder.setOnInfoListener(null);
        mediaRecorder.stop();
//        mediaRecorder.reset();
        mediaRecorder.release();
        mediaRecorder = null;
    }

    public static void release(MediaRecorder mediaRecorder){
        if (null == mediaRecorder){
            printMessage("mediaRecorder can not null !");
            return;
        }
        mediaRecorder.release();
        mediaRecorder = null;
    }

    /**
     * 获取视频文件
     * @return
     */
    public static File getFile() {
        return file;
    }

    private static void printMessage(String msg){
        if (DEBUG){
            Log.i(TAG,msg);
        }
    }


}
